# -*-coding:utf-8-*-
import os
import sys

from config import get_devices, is_number, get_package
from logTest import logger


# adb -s 设备名 shell monkey -p 包名 --pct-syskeys 0 --pct-touch 百分比 -s 伪随机数 -v -v -v 循环多少次>./a.txt
def run_monkey():

    if len(get_devices()) == 0:
        print(os.popen("adb devices").readlines())
        input("设备未认证\n点击任意键结束进程>>>>>>")
        sys.exit()

    print("\n******为能准确获取包名，请先打开待测app并置于前端******\n")
    print("当前正在运行的设备及包名分别有:",get_package()[0],"\n")

    packageName = input("请输入执行monkey命令的包名：")
    randomNumber = input("请输入伪随机数：")
    randomNumber = is_number(randomNumber)
    intervalTime = input("请输入间隔时间ms：")
    intervalTime = is_number(intervalTime)
    count = input("请输入事件执行次数：")
    count = is_number(count)
    touchEvent = input("请输入触碰事件百分比：")
    touchEvent = is_number(touchEvent)
    motionEvent = input("请输入手势事件百分比：")
    motionEvent = is_number(motionEvent)
    zoomEvent = input("请输入缩放事件百分比：")
    zoomEvent = is_number(zoomEvent)
    trackEvent = input("请输入轨迹事件百分比：")
    trackEvent = is_number(trackEvent)
    rotationEvent = input("请输入屏幕旋转事件百分比：")
    rotationEvent = is_number(rotationEvent)
    navEvent = input("请输入导航事件百分比：")
    navEvent = is_number(navEvent)
    majornavEvent = input("请输入主要导航事件百分比：")
    majornavEvent = is_number(majornavEvent)
    switchEvent = input("请输入启动activity事件百分比：")
    switchEvent = is_number(switchEvent)
    flipEvent = input("请输入键盘事件百分比：")
    flipEvent = is_number(flipEvent)
    anyEvent = input("请输入其他事件百分比：")
    anyEvent = is_number(anyEvent)
    # 发生异常后是否杀死进程还未写
    error = input("是否同意发生异常后杀死进程？（yes/no）：")

    for dp in get_package():
        if type(randomNumber) == int and type(count) == int and type(touchEvent) == int \
                and type(motionEvent) == int and type(zoomEvent) == int and type(trackEvent) == int \
                and type(rotationEvent) == int and type(navEvent) == int and type(majornavEvent) ==int\
                and type(switchEvent) ==int and type(flipEvent) == int and type(anyEvent) == int:
            if touchEvent+motionEvent+zoomEvent+trackEvent+rotationEvent+navEvent+majornavEvent+switchEvent+flipEvent+anyEvent == 100:
                basicCommand = 'adb -s {} shell monkey -p {} --pct-syskeys 0'.format(dp[0],packageName)
                controlEvent1 = ' --pct-touch {} --pct-motion {} --pct-pinchzoom {} --pct-trackball {}'.format(touchEvent,motionEvent,zoomEvent,trackEvent)
                controlEvent2 = ' --pct-rotation {} --pct-nav {} --pct-majornav {} --pct-appswitch {}'.format(rotationEvent,navEvent,majornavEvent,switchEvent,)
                controlEvent3 = ' --pct-flip {} --pct-anyevent {}'.format(flipEvent,anyEvent)
                anomal = ' --ignore-crashes --ignore-timeouts --monitor-native-crashes --ignore-security-exceptions'
                interval = ' -s {} {} --throttle {}  -v -v -v {}'.format(randomNumber,anomal,intervalTime,count)
                if error == 'yes':
                    logger.info(basicCommand + controlEvent1 + controlEvent2 + controlEvent3 + ' --kill-process-after-error' + interval + '>D://monkeyTest//monkey.log')
                    os.system(basicCommand + controlEvent1 + controlEvent2 + controlEvent3 + ' --kill-process-after-error' + interval + '>D://monkeyTest//monkey.log')
                else:
                    logger.info(basicCommand + controlEvent1 + controlEvent2 + controlEvent3 + interval + '>D://monkeyTest//monkey.log')
                    os.system(basicCommand + controlEvent1 + controlEvent2 + controlEvent3 + interval + '>D://monkeyTest//monkey.log')
            else:
                print("\n*******各类事件的百分比之和需要等于100才可正常执行********")
        else:
            print("\n数值类型有误")


